package mutipackage;

import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class AsResponseEncoder extends ProtocolEncoderAdapter {  
    private final Charset charset;  
      
    public AsResponseEncoder(Charset charset){  
        this.charset = charset;  
    }  
      
    public void encode(IoSession session, Object message,  
        ProtocolEncoderOutput out) throws Exception {  
        IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true);  
        String content = (String) message ; 
        byte[] cbytes = content.getBytes(this.charset) ;
        buffer.putInt(cbytes.length+ServerConfiguration.MSG_HEADER_LEN);//将前4位设置成数据体的字节长度  
        buffer.put(cbytes);//消息内容  
        buffer.flip();  
        out.write(buffer);
    }  
}  